package com.study.living.camera;

import android.content.Context;
import android.hardware.Camera;

import androidx.collection.SparseArrayCompat;

import java.util.SortedSet;

public class Camera1 extends AbsCamera{

    private static final int INVALID_CAMERA_ID = -1;

    private static final SparseArrayCompat<Integer> CAMERA_FACING = new SparseArrayCompat<>();

    static {
        CAMERA_FACING.put(FACING_FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT);
        CAMERA_FACING.put(FACING_BACK, Camera.CameraInfo.CAMERA_FACING_BACK);
    }

    private final Camera.CameraInfo mCameraInfo = new Camera.CameraInfo();

    private Camera mCamera;
    private int mCameraId;


    public Camera1(Context context, TexturePreview texturePreview) {
        super(context, texturePreview);
        mPreview.setOnSurfaceAvailableListener(new TexturePreview.OnSurfaceAvailableListener() {
            @Override
            public void onSurfaceAvailable() {
                startCaptureSession();
            }

            @Override
            public void onSurfaceDestroyed() {
                stop();
            }
        });
    }

    @Override
    public boolean start() {
        return false;
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean chooseCameraByFacing() {
        Integer facing = CAMERA_FACING.get(mFacing, INVALID_CAMERA_ID);
        if (facing != INVALID_CAMERA_ID){
            for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
                Camera.getCameraInfo(i, mCameraInfo);
                if (mCameraInfo.facing == mFacing) {
                    mCameraId = i;
                    return true;
                }
            }
        }
        mCameraId = INVALID_CAMERA_ID;
        return false;
    }

    @Override
    public void openCamera() {
        if (mCamera != null) {
            releaseCamera();
        }
        mCamera = Camera.open(mCameraId);
        adjustCameraParameters();
        mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
    }

    private void adjustCameraParameters() {
        SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
        if (sizes == null) { // Not supported
            mAspectRatio = chooseAspectRatio();
            sizes = mPreviewSizes.sizes(mAspectRatio);
        }
        Size size = chooseOptimalSize(sizes);

        // Always re-apply camera parameters
        // Largest picture size in this ratio
        final Size pictureSize = mPictureSizes.sizes(mAspectRatio).last();
        if (mShowingPreview) {
            mCamera.stopPreview();
        }
        mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
        mCameraParameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
        mCameraParameters.setRotation(calcCameraRotation(mDisplayOrientation));
        setAutoFocusInternal(mAutoFocus);
        setFlashInternal(mFlash);
        mCamera.setParameters(mCameraParameters);
        if (mShowingPreview) {
            mCamera.startPreview();
        }
    }

    @Override
    public boolean isCameraOpened() {
        return false;
    }


    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

}
